package com.ibuonline.b2b.shop.dao;

import java.io.File;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.dimeng.common.base.SuperDAO;
import com.ibuonline.b2b.common.Constants;
import com.ibuonline.b2b.common.ResourceUtil;
import com.ibuonline.b2b.entity.ShopAlbum;
import com.ibuonline.b2b.entity.ShopAlbumPhoto;

public class ShopAlbumDAO extends SuperDAO<ShopAlbum>{
	
	public int countMax(String sql, Object[] fieldValues) {
		return jdbcTemplate.queryForInt(sql, fieldValues);
	}
	
	@SuppressWarnings("unchecked")
	public Boolean deleteAlbum(final Integer albumId, final ShopAlbum defaultAlbum) {
		return (Boolean)template.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Transaction transaction = null;
				try{
					transaction = session.beginTransaction();
					if(defaultAlbum.getId() == null) {
						session.save(defaultAlbum);
					}
					Query query = session.createQuery("update ShopAlbumPhoto sap set sap.albumId = ? where sap.albumId=?");
					query.setInteger(0, defaultAlbum.getId());
					query.setInteger(1, albumId);
					query.executeUpdate();
					session.delete(session.get(ShopAlbum.class, albumId));
					transaction.commit();
					return true;
				}catch(Exception e) {
					if(transaction != null)
						transaction.rollback();
					return false;
				}
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	public Boolean uploadMoreShopPhoto(final List<File> shopPhotos, final List<String> shopPhotosFileName, final Integer albumId, final boolean waterMark){
		return (Boolean)template.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Transaction transaction = null;
				try{
					transaction = session.beginTransaction();
					for(int i=0; i<shopPhotos.size(); i++) {
						File file = shopPhotos.get(i);
						String fileName = shopPhotosFileName.get(i);
						Timestamp now = new Timestamp(System.currentTimeMillis());
						ShopAlbumPhoto sp = new ShopAlbumPhoto();
						sp.setAlbumId(albumId);
						sp.setPath(null);
						sp.setStatus(Constants.SHOP_PHOTO_UPLOAD_NOTEDIT_STATUS);
						sp.setCreateTime(now);
						sp.setLastupdate(now);
						session.save(sp);
						String photoPath = ResourceUtil.saveFile(file, Constants.RES_TYPE_PHOTO, 
								sp.getId()+fileName.substring(fileName.lastIndexOf(".")), waterMark, true);
						sp.setPath(photoPath);
						sp.setTitle(sp.getId()+"");
						session.save(sp);
					} 
					transaction.commit();
					return true;
				}catch(Exception e) {
					if(transaction != null)
						transaction.rollback();
					return false;
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	public Boolean changeUploadShopPhotoStatus(Integer id) {
		return (Boolean)template.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Transaction transaction = null;
				try{
					transaction = session.beginTransaction();
					Query query = session.createQuery("update ShopAlbumPhoto sap set sap.status=? where sap.status=?");
					query.setString(0, Constants.SHOP_PHOTO_NORMAL_STATUS);
					query.setString(1, Constants.SHOP_PHOTO_UPLOAD_NOTEDIT_STATUS);
					query.executeUpdate();
					transaction.commit();
					return true;
				}catch(Exception e) {
					if(transaction != null)
						transaction.rollback();
					return false;
				}
			}
		});
	}
}
